<template>
  <div class="app-container">
    <el-card shadow="never">
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item>
          <el-button type="primary" @click="handleFilter" class="search-button">查询</el-button>
          <el-button type="primary" @click="showAdd" class="search-button">添加</el-button>
          <el-button type="primary" @click="goBack" class="search-button">返回</el-button>
        </el-form-item>
      </el-form>

      <el-table v-loading="listLoading" :data="list" element-loading-text="Loading" fit highlight-current-row>
        <el-table-column align="center" type="index" label="序号" width="55">
        </el-table-column>
        <el-table-column label="名称" align="center" prop="questionName">
        </el-table-column>
        <el-table-column label="类型" align="center" prop="questionType">
        </el-table-column>
        <el-table-column label="选项" align="center" prop="questionOptions">
          <template slot-scope="{row}">
            <div v-for="item in row.questionOptions" :key="item.options">
              <span>{{ item.options }}</span>:
              <span>{{ item.value }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" width="200">
          <template slot-scope="{row}">
            <el-button @click="showEdit(row)" type="text">编辑</el-button>
            <el-button @click="delHandle(row)" type="text">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total > 0" :total="total" :page.sync="searchForm.pageIndex" :limit.sync="searchForm.pageSize"
        @pagination="getList" />
    </el-card>
    <el-dialog :title="dialogTit" :visible.sync="dialogVisible" width="40%">
      <el-form label-position="right" label-width="120px" ref="form" :model="form">
        <el-form-item label="名称">
          <el-input v-model.trim="form.questionName"></el-input>
        </el-form-item>
        <el-form-item label="类型">
          <el-select v-model="form.questionType" placeholder="请选择">
            <el-option v-for="item in ['单选', '多选', '填空']" :key="item" :label="item" :value="item">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选项" prop="questionAnswer" v-if="form.questionType === '单选' || form.questionType === '多选'">
          <div style="display: flex;margin-bottom:20px ;" v-for="item in choice" :key="item.options">
            <span style="margin-right: 20px;">{{ item.options }}</span>
            <el-input v-model="item.value" />
          </div>
          <div>
            <el-button @click="addChoice">增加选项</el-button>
            <el-button @click="choice.pop()">删除选项</el-button>
          </div>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" class="btn-cancel">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import * as FieldEvaluation from '@/api/FieldEvaluation'
import Pagination from '@/components/Pagination'
export default {
  components: { Pagination },
  data () {
    return {
      list: null,
      listLoading: true,
      searchForm: {
        pageIndex: 1,
        pageSize: 10
      },
      total: 0,
      dialogVisible: false,
      dialogTit: '',
      form: {},
      letter: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
        'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'],
      choice: [
        { options: 'A', value: "" },
        { options: 'B', value: "" },
        { options: 'C', value: "" },
        { options: 'D', value: "" },
      ],
    }
  },
  created () {
    this.searchForm.evaluationUid = this.$route.query.uid
    this.getList()
  },
  methods: {
    getList () {
      this.listLoading = true
      const param = Object.assign({}, this.searchForm)
      FieldEvaluation.queryByEvaUid(param).then(response => {
        this.list = response.data.rows || []
        if (this.list.length > 0) {
          this.list.map(item => {
            if (item.questionType !== '填空') {
              item.questionOptions = JSON.parse(item.questionOptions)
            }
          })
        }
        this.total = response.data.total
        this.listLoading = false
      }).catch(error => {
        this.listLoading = false
    console.log(error)
      })
    },
    handleFilter () {
      this.searchForm.pageIndex = 1
      this.getList()
    },
    handleReset () {
      this.resetTemp();
      this.getList();
    },
    resetTemp () {
      this.searchForm = {
        pageIndex: 1,
        pageSize: 10
      }
    },
    showAdd () {
      this.dialogTit = '新增'
      this.form = {}
      this.choice = [
        { options: 'A', value: "" },
        { options: 'B', value: "" },
        { options: 'C', value: "" },
        { options: 'D', value: "" },
      ]
      this.dialogVisible = true
    },
    showEdit (row) {
      this.dialogTit = '编辑'
      this.form = Object.assign({}, row)
      this.choice = this.form.questionOptions
      delete this.form.createTime
      delete this.form.updateTime
      this.dialogVisible = true
    },
    submit () {
      if (this.form.questionType === '单选' || this.form.questionType === '多选') {
        this.form.questionOptions = JSON.stringify(this.choice)
      }
      if (this.dialogTit === '新增') {
        this.creatHandle()
      } else if (this.dialogTit === '编辑') {
        this.editHandle()
      }
    },
    creatHandle () {
      this.form.evaluationUid = this.$route.query.uid
      FieldEvaluation.insertQuestion(this.form).then(response => {
        if (response.code == 200) {
          this.dialogVisible = false
          this.getList()
          this.$message.success('新增成功')
        }
      }).catch(error => {
    console.log(error)
      })
    },
    editHandle () {
      FieldEvaluation.updateQuestion(this.form).then(response => {
        if (response.code == 200) {
          this.dialogVisible = false
          this.getList()
          this.$message.success('修改成功')
        }
      }).catch(error => {
    console.log(error)
      })
    },
    delHandle (data) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const param = {
          uid: data.uid,
        }
        FieldEvaluation.deleteQuestion(param).then(result => {
          this.$message.success(result.msg)
          this.getList()
        }).catch(error => {
          this.loading = false
          console.log(error)
        })
      }).catch(() => {
        console.log('取消删除')
      });
    },
    goBack () {
      this.$router.go(-1)
    },
    addChoice () {
      let num = this.choice.length
      let options = this.letter[num]
      this.choice.push({
        options: options, value: ""
      })
    },
  }
}
</script>
